//
//  BaseHttpRequest.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/10.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "BaseHttpRequest.h"
#import "AFNetworking.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <errno.h>
#include <net/if_dl.h>

#include <ctype.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#import <CommonCrypto/CommonDigest.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sockio.h>

typedef void (^AFResponseSuccess)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject);
typedef void (^AFResponseFailed)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error);
@implementation BaseHttpRequest
//MD5转换
+ (NSString *)md5HexDigest:(NSString*)input
{
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, strlen(str), result);
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];//
    
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}
+(void)requestJSONWithUrl:(NSString *)url param:(NSDictionary *)param method:(AFRequestMethod)method completion:(RequestCompletionBlock)completion
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", @"text/plain", nil];

    
    //    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    AFResponseSuccess successBlock = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        if ([responseObject isKindOfClass:[NSDictionary class]] && responseObject[@"code"] != nil && [responseObject[@"code"] integerValue] != 200 && responseObject[@"msg"] != nil) {
            completion(NO, responseObject[@"msg"]);
        } else {
            completion(YES, responseObject);
        }
    };
    
    AFResponseFailed failedBlock = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completion(NO, nil);
    };
    switch (method) {
        case AFRequestMethodGET:
        {
            [session GET:url parameters:param progress:nil success:successBlock failure:failedBlock];
        }
            break;
        case AFRequestMethodPOST:
        {
            [session POST:url parameters:param progress:nil success:successBlock failure:failedBlock];
        }
            break;
        case AFRequestMethodPUT:
        {
            [session PUT:url parameters:param success:successBlock failure:failedBlock];
        }
            break;
        default:
        {
            [session DELETE:url parameters:param success:successBlock failure:failedBlock];
        }
            break;
    }
}

+ (void)requestCodeWithUrl:(NSString *)url param:(NSDictionary *)param method:(AFRequestMethod)method completion:(RequestCodeCompletionBlock)completion
{

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", @"text/plain", nil];


    //    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    AFResponseSuccess successBlock = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        if ([responseObject isKindOfClass:[NSDictionary class]] && responseObject[@"code"] != nil && [responseObject[@"code"] integerValue] != 200 && responseObject[@"msg"] != nil) {
            completion(NO, responseObject[@"code"], responseObject[@"msg"]);
        } else {
            completion(YES,@"200", responseObject);
        }
    };
    AFResponseFailed failedBlock = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completion(NO,@"500", nil);
    };
    switch (method) {
        case AFRequestMethodGET:
        {
            [session GET:url parameters:param progress:nil success:successBlock failure:failedBlock];
        }
            break;
        case AFRequestMethodPOST:
        {
            [session POST:url parameters:param progress:nil success:successBlock failure:failedBlock];
        }
            break;
        case AFRequestMethodPUT:
        {
            [session PUT:url parameters:param success:successBlock failure:failedBlock];
        }
            break;
        default:
        {
            [session DELETE:url parameters:param success:successBlock failure:failedBlock];
        }
            break;
    }
}

+(void)loginWithURL:(NSString *)serverUrl
         parameters:(NSMutableDictionary *)paras
            success:(void (^)(id *dicData))success
               fail:(void (^)(NSError *error))fail{
    [self requestCodeWithUrl:serverUrl param:paras method:AFRequestMethodGET completion:^(BOOL isSuccess, NSString *code, id object) {
       
    }];
}

- (NSString *)requestMethod:(AFRequestMethod)method
{
    switch (method) {
        case AFRequestMethodGET:
            return @"GET";
            break;
        case AFRequestMethodPOST:
            return @"POST";
            break;
        case AFRequestMethodPUT:
            return @"PUT";
            break;
        case AFRequestMethodDELETE:
            return @"DELETE";
            break;
            
        default:
            break;
    }
    return nil;
}

@end
